<?php

namespace App\Console\Commands\Cruise;

use App\Models\Common\ConfigModel;
use App\Models\Cruise\CruiseCheckAlbumModel;
use App\Models\Cruise\CruiseShopLicenseRecordsModel;
use App\Models\Cruise\CruiseShopLicenseResourceRecordsModel;
use App\Models\Shop\ShopModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\System\SystemNotifyService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class CruiseCheckLicenceExpiredNotify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:check_licence_expired_notify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '稽核脚本查询过期证件照通知';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws GuzzleException
     */
    public function handle(): int
    {
        $messageId = 163134;

        CruiseShopLicenseRecordsModel::query()
                                     ->leftJoin('kf_shops', 'kf_shops.id', 'cruise_shop_license_records.shopId')
                                     ->whereIn('cruise_shop_license_records.status',
                                         [
                                             CruiseShopLicenseRecordsModel::STATUS_LACK,
                                             CruiseShopLicenseRecordsModel::STATUS_EXPIRED,
                                             CruiseShopLicenseRecordsModel::STATUS_REJECT
                                         ])
                                     ->where('kf_shops.status', ShopModel::STATUS_OK)
                                     ->chunkById(100, function ($item) use ($messageId) {
                                         $shopIds = $item->pluck('shopId')->unique()->values()->toArray();

                                         $rmsShopManagers = RmsUserRemote::getShopManagerUserByShopId($shopIds)['lists'] ?? [];
                                         $rmsShopManagers = array_column($rmsShopManagers, 'userId');
                                         $rmsShopManagers = array_values(array_unique($rmsShopManagers));

                                         SystemNotifyService::preSend($messageId, $rmsShopManagers);

                                     }, 'cruise_shop_license_records.id');

        return 0;
    }
}
